var Excel = require('exceljs');
const xlsx = require('node-xlsx');

exports.mkExl = function (data) {

  var start_time = new Date();
  var workbook = new Excel.stream.xlsx.WorkbookWriter({
    filename: './SD_DATA_201901242.xlsx'
  });
  var worksheet = workbook.addWorksheet('Sheet');

  worksheet.columns = [
    {
      header: 'model',
      key: 'model'
    },
    {
      header: 'rank',
      key: 'rank'
    },
    {
      header: 'ID',
      key: 'ID'
    },
    {
      header: 'coding',
      key: 'coding'
    },
    {
      header: 'Name',
      key: 'Name'
    },
    {
      header: 'att',
      key: 'att'
    },
    {
      header: 'def',
      key: 'def'
    },
    {
      header: 'fly',
      key: 'fly'
    },
    {
      header: 'ctl',
      key: 'ctl'
    },
    {
      header: 'D4sum',
      key: 'D4sum'
    },
    {
      header: 'D3sum',
      key: 'D3sum'
    },
    {
      header: 'landType',
      key: 'landType'
    },
    {
      header: 'fightType',
      key: 'fightType'
    },
    {
      header: 'story',
      key: 'story'
    },
    {
      header: 'force',
      key: 'force'
    },
    {
      header: 'feature',
      key: 'feature'
    },
    {
      header: 'from',
      key: 'from'
    },
    {
      header: 'pilot',
      key: 'pilot'
    },
    {
      header: 'Machine',
      key: 'Machine'
    },
    {
      header: 'MID',
      key: 'MID'
    },
    {
      header: 'RID',
      key: 'RID'
    },
    {
      header: 'fever',
      key: 'fever'
    }
  ];

  // var data = [{
  //   id: 100,
  //   name: 'abc',
  //   phone: '123456789'
  // }];
  var length = data.length;

  // 当前进度
  var current_num = 0;
  var time_monit = 400;
  var temp_time = Date.now();

  console.log('开始添加数据');
  // 开始添加数据
  for (let i in data) {
    worksheet.addRow(data[i]).commit();
    current_num = i;
    if (Date.now() - temp_time > time_monit) {
      temp_time = Date.now();
      console.log((current_num / length * 100).toFixed(2) + '%');
    }
  }
  console.log('添加数据完毕：', (Date.now() - start_time));
  workbook.commit();

  var end_time = new Date();
  var duration = end_time - start_time;

  console.log('用时：' + duration);
  console.log("程序执行完毕");

}

exports.importExl = function () {
  const xlsxfile = __dirname + "/SD Data Stream 20190116.xlsx";

  let obj = xlsx.parse(xlsxfile);
  console.log(obj)
  return obj[0].data
}